#!/bin/bash
set -e

if [ "$(id -u)" -eq 0 ]; then
  if ! grep -E "^app" /etc/group > /dev/null 2>&1; then
    groupadd app
  fi

  if ! grep -E "^app" /etc/passwd > /dev/null 2>&1; then
    useradd -d /app -g app app
  fi

  uid=$(id -u app)
  gid=$(id -g app)

  chown -R "${uid}:${gid}" /app
  chmod 777 /tmp

  exec gosu "${uid}:${gid}" "$@"
fi

exec "$@"

